/*
Copyright (c) 2008,2009, David Beck

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
   @file t__demo1_server.cc
   @brief verify the server side of rt0d4/Rx security model

   @file t__demo1.ss.c
   @brief Automatically generated by rxgen
*/

#define T__DEMO1_SERVER_PORT 11984
#define T__DEMO1_SERVICE_ID 4

#include "rt0d4_rx.h"
#include "zfile.hh"
#include <unistd.h>
#include <assert.h>

RT0D4_CDECL
afs_int32 t__demo1_ExecuteRequest(register struct rx_call *z_call);

RT0D4_CDECL
int t__demo1_Multiply(
	/*IN */ struct rx_call *z_call,
	/*IN */ double a,
	/*IN */ double b,
	/*OUT*/ double * r) {
  *r = a * b;
  return 0;
}

using rt0d4::common::zfile;

/** @brief Verifies rt0d4/Rx security model */
namespace test_rt0d4_rx {

RT0D4_CDECL
int validate_peer_cert_cb(
        const unsigned char * ccert, size_t len)
{
  assert( ccert != NULL && len > 0 );
  zfile zf;
  unsigned char cert[4000];
  zf.read_file("../../rt0d4_sec/test/client/client.crt");
  zf.get_data(cert);
  assert( len == zf.get_size() );
  assert( memcmp(cert,ccert,len) == 0 );
  return 0;
}

RT0D4_CDECL
int validate_peer_credentials_cb(
        const char * login,
        const char * password)
{
  assert( login && password );
  assert( strcmp("Login",login) == 0 );
  assert( strcmp("Pass",password) == 0 );
  return 0;
}

/**
    @test Test server to verify the rt0d4/Rx security model
*/
int test_server(int argc, char ** argv)
{
  const char * errstr = "OK";
  struct rx_securityClass *(sec_p[1]);
  struct rx_service * service_p = 0;
  unsigned short svr_port = T__DEMO1_SERVER_PORT;
  zfile zf;
  unsigned char cert[4000];
  const char * algs[] = { "c2tnb239v2", 0 };

  if (rx_Init(htons(svr_port))) {
    errstr ="rx_Init";
    goto error;
  }

  if (!(sec_p[0] = rt0d4_rx_NewServerSecurityObject() )) {
    errstr = "NewServerSecurityObject";
    goto error;
  }

  zf.read_file("../../rt0d4_sec/test/server/server.crt");
  zf.get_data(cert);
  rt0d4_rx_SetOwnCert(*sec_p,cert,zf.get_size());
  rt0d4_rx_AddAlgorithms(*sec_p, algs);
  rt0d4_rx_SetEcKeyStrength(*sec_p, 239, 239 );
  rt0d4_rx_SetPeerCertValidateCallBack(*sec_p, validate_peer_cert_cb);
  rt0d4_rx_SetPeerCredentialsValidateCallBack(*sec_p, validate_peer_credentials_cb);

  if (!(service_p = rx_NewService(
                     0,                   /* port */
                     T__DEMO1_SERVICE_ID,
                     "t__demo1",          /* service name */
                     sec_p,               /* secobjs */
                     1,                   /* n secobjs */
                     t__demo1_ExecuteRequest))) {
    errstr = "NewService";
    goto error;
  }
  rx_StartServer(1);

  rx_Finalize();
  printf("OK: %s\n",errstr);
  return 0;

error:
  printf("ERROR: %s\n",errstr);
  return 0;
}

} // end of test_rt0d4_rx

using namespace test_rt0d4_rx;

int main(int argc, char ** argv)
{
  return test_server(argc, argv);
}

/* EOF */
